//
//  ScreenRulerUnitTileView.m
//  ScreenRuler
//
//  Created by Tianzhou on 08-12-24.
//  Copyright 2008 Tianzhou Software Inc.. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.

#import "ScreenRulerUnitTileView.h"

@implementation ScreenRulerUnitTileView

- (id)initWithFrame:(NSRect)frameRect enclosingScrollView:(NSScrollView *)theEnclosingScrollView
{
	if( (self = [super initWithFrame:frameRect]) != nil )
	{
		enclosingScrollView_ = theEnclosingScrollView;
		NSString *theMeasurementUnit = [[enclosingScrollView_ horizontalRulerView] measurementUnits];
		if ( [theMeasurementUnit compare:@"Inches"] )
		{
			currentMeasurementUnit_ = kScreenRulerInches;
		}
		else if ( [theMeasurementUnit compare:@"Centimeters"] )
		{
			currentMeasurementUnit_ = kScreenRulerCentimeters;
		}
		else
		{
			currentMeasurementUnit_ = kScreenRulerPoints;
		}
	}
	return self;
}

- (void)drawRect:(NSRect)theRect
{
	NSString *theUnitString;
	switch ( currentMeasurementUnit_ )
	{
		case kScreenRulerInches:
		{
			theUnitString = @"in";
			break;
		}
		case kScreenRulerCentimeters:
		{
			theUnitString = @"cm";
			break;
		}
		case kScreenRulerPoints:
		{
			theUnitString = @"px";
			break;
		}
		default:
		{
			theUnitString = @"px";
			break;
		}
	}
	
	NSRulerView *theHorzRuler = [enclosingScrollView_ horizontalRulerView];
	CGContextRef theContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
	CGContextSetTextMatrix(theContext, CGAffineTransformIdentity);
	CGContextSelectFont (theContext,
						 "Times",
						 [theHorzRuler ruleThickness]*4/5,
						 kCGEncodingMacRoman);
	CGContextSetCharacterSpacing(theContext, 1);
	CGContextSetTextDrawingMode(theContext,kCGTextFill);
	CGContextShowTextAtPoint(theContext,
							 0,
							 [theHorzRuler ruleThickness]/5,
							 [theUnitString cStringUsingEncoding:NSASCIIStringEncoding],
							 [theUnitString length]);
	CGContextStrokePath(theContext);
}

- (void)mouseDown:(NSEvent *)theEvent
{
	NSRulerView *theHorzRuler = [enclosingScrollView_ horizontalRulerView];
	NSRulerView *theVertRuler = [enclosingScrollView_ verticalRulerView];
	
	++currentMeasurementUnit_;
	currentMeasurementUnit_ = currentMeasurementUnit_ % kScreenRulerMeasurementUnitKind;
	switch ( currentMeasurementUnit_ )
	{
		case kScreenRulerInches:
		{
			[theHorzRuler setMeasurementUnits:@"Inches"];
			[theVertRuler setMeasurementUnits:@"Inches"];
			break;
		}
		case kScreenRulerCentimeters:
		{
			[theHorzRuler setMeasurementUnits:@"Centimeters"];
			[theVertRuler setMeasurementUnits:@"Centimeters"];
			break;
		}
		case kScreenRulerPoints:
		{
			[theHorzRuler setMeasurementUnits:@"Points"];
			[theVertRuler setMeasurementUnits:@"Points"];
			break;
		}
		default:
		{
			[theHorzRuler setMeasurementUnits:@"Points"];
			[theVertRuler setMeasurementUnits:@"Points"];
			break;
		}
	}
	[[self window] mouseDown:theEvent];
}

@end
